package com.price.compare.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.price.compare.common.JsonResult;
import com.price.compare.common.util.ResultUtil;
import com.price.compare.entity.SysMenuEntity;
import com.price.compare.entity.SysUserEntity;
import com.price.compare.entity.SysUserRoleEntity;
import com.price.compare.service.SysMenuService;
import com.price.compare.security.entity.SelfUserEntity;
import com.price.compare.service.SysRoleService;
import com.price.compare.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 普通用户
 * @Author cabbage
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 用户端信息
     * @Author cabbage
     * @Return Map<String,Object> 返回数据MAP
     */
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public Map<String,Object> userLogin(){
        Map<String,Object> result = new HashMap<>();
        SelfUserEntity userDetails = (SelfUserEntity) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
        result.put("title","用户端信息");
        result.put("data",userDetails);
        return ResultUtil.resultSuccess(result);
    }

    /**
     * 拥有USER角色和sys:user:info权限可以访问
     * @Author cabbage
     * @Return Map<String,Object> 返回数据MAP
     */
    @PreAuthorize("hasRole('USER') and hasPermission('/user/menuList','sys:user:info')")
    @RequestMapping(value = "/menuList",method = RequestMethod.GET)
    public Map<String,Object> sysMenuEntity(){
        Map<String,Object> result = new HashMap<>();
        List<SysMenuEntity> sysMenuEntityList = sysMenuService.list();
        result.put("title","拥有USER角色和sys:user:info权限可以访问");
        result.put("data",sysMenuEntityList);
        return ResultUtil.resultSuccess(result);
    }

    /**
     * 用户登录
     */

    /**
     * 修改密码
     * @Author cabbage
     */
//    @PreAuthorize("hasRole('USER') and hasPermission('/user/menuList','sys:user:info')")
    @GetMapping(value = "/change-password")
    public JsonResult changePassword(@RequestBody SysUserEntity sysUserEntity){
        SysUserEntity sysUserEntityInDb = sysUserService.selectUserByName(sysUserEntity.getUsername());
        UpdateWrapper<SysUserEntity> userEntityUpdateWrapper = null;
        //旧密码和数据库密码相同
        if (sysUserEntity.getOldPassword().equals(sysUserEntityInDb.getPassword())){
            userEntityUpdateWrapper = new UpdateWrapper<>();
            userEntityUpdateWrapper.lambda().set(SysUserEntity::getPassword, bCryptPasswordEncoder.encode(sysUserEntity.getPassword()));
        }
        return sysUserService.update(userEntityUpdateWrapper) ? JsonResult.ok("修改密码成功") : JsonResult.error("修改密码失败");
    }



}
